﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Road3App.Model.CrossroadNs;

namespace Road3App.Views.UserControls
{
    /// <summary>
    /// Interaction logic for CrossroadUC.xaml
    /// </summary>
    public partial class CrossRoadUC : TransformControl
    {
        #region Properties

        public bool ShowGrid
        {
            get { return (bool)GetValue(ShowGridProperty); }
            set { SetValue(ShowGridProperty, value); }
        }
        public static readonly DependencyProperty ShowGridProperty =
           DependencyProperty.Register("ShowGrid", typeof(bool), typeof(CrossRoadUC), new PropertyMetadata(ShowGridChangedHandler));
        
        public static void ShowGridChangedHandler(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ((CrossRoadUC)d).LayoutRoot.ShowGridLines = (bool)e.NewValue;
        }


        //public bool ShowCellStates
        //{
        //    get { return (bool)GetValue(ShowCellStatesProperty); }
        //    set { SetValue(ShowCellStatesProperty, value); }
        //}
        //public static readonly DependencyProperty ShowCellStatesProperty =
        //    DependencyProperty.Register("ShowCellStates", typeof(bool), typeof(CrossRoadUC), new UIPropertyMetadata(ShowCellStatesChangedHandler));

        //private static void ShowCellStatesChangedHandler(DependencyObject d, DependencyPropertyChangedEventArgs e)
        //{
        //    ((CrossRoadUC)d).LayoutRoot.Children.OfType<CellUC>().ToList()
        //        .ForEach(c => c.ShowState = (bool)e.NewValue ? Visibility.Visible : Visibility.Collapsed);
        //}

        #endregion

        public CrossRoadUC()
        {
            InitializeComponent();

            this.DataContextChanged += (s, ea) => this.UpdateGrid();
            LayoutRoot.ShowGridLines = ShowGrid;
        }

        public CrossRoadUC(CrossRoad cr)
            : this()
        {
            this.DataContext = cr;
        }

        public void UpdateGrid()
        {
            var cr = this.DataContext as CrossRoad;
            if (cr == null)
                return;

            LayoutRoot.Children.Clear();
            LayoutRoot.RowDefinitions.Clear();
            LayoutRoot.ColumnDefinitions.Clear();

            for (int i = 0; i < cr.width + 2; i++)
                LayoutRoot.ColumnDefinitions.Add(new ColumnDefinition());
            for (int i = 0; i < cr.height + 2; i++)
                LayoutRoot.RowDefinitions.Add(new RowDefinition());

            foreach (var cell in cr.Grid)
            {
                CellUC cellUC = new CellUC(cell);
                //cellUC.ShowState = this.ShowCellStates ? Visibility.Visible : Visibility.Collapsed;
                cellUC.SetValue(Grid.ColumnProperty, cell.Coords.X);
                cellUC.SetValue(Grid.RowProperty, cell.Coords.Y);
                LayoutRoot.Children.Add(cellUC);
            }
        }
    }
}
